package ee.maoinvest.frontend.controller.command.enterpriseCommands;

import java.sql.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.services.LatestDatesService;
import ee.maoinvest.backend.services.implementations.LatestDatesServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriseforms.InsertEnterpriseFormConverter;

public class FillInsertEnterpriseFormConverterWithInitialDataCommand implements Command
{
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int PROBABLY_A_DATABASE_ERROR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		InsertEnterpriseFormConverter insertEnterpriseFormConverter = null;
		LatestDatesService latestDatesServiceImp = null;
		Date latestDateAfterWhatDataManipulationIsPossible = null;
		
		try 
		{
			insertEnterpriseFormConverter = new InsertEnterpriseFormConverter();
			latestDatesServiceImp = new LatestDatesServiceImpl();
			latestDateAfterWhatDataManipulationIsPossible = latestDatesServiceImp.getLatestDateAfterWhatDataManipulationIsPossible();
			insertEnterpriseFormConverter.fillWithInitialData(latestDateAfterWhatDataManipulationIsPossible);
			request.setAttribute("insertEnterpriseFormConverter", insertEnterpriseFormConverter);
			
			HttpSession session = request.getSession();
			session.setAttribute("latestDateAfterWhatDataManipulationIsPossible", latestDateAfterWhatDataManipulationIsPossible);
			
			operationResult = OPERATION_SUCCESSFUL;
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			return PROBABLY_A_DATABASE_ERROR;
		}
		
		return operationResult;
	}
}
